/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.filter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.shishuo.cdn.entity.User;
import com.shishuo.cdn.util.ConfigUtil;
import com.shishuo.cms.constant.ConfigConstant;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.vo.ArticleVo;
import com.shishuo.cms.entity.vo.HeadlineVo;
import com.shishuo.cms.service.ArticleService;
import com.shishuo.cms.service.ConfigService;
import com.shishuo.cms.service.FolderService;
import com.shishuo.cms.service.HeadlineService;
import com.shishuo.cms.util.HttpUtils;

/**
 * @author Herbert
 * 
 */
@Component
public class GlobalInterceptor implements HandlerInterceptor {

	@Autowired
	private ConfigService configService;
	@Autowired
	private  HeadlineService headlineService;
	@Autowired
	private  ArticleService articleService;
	@Autowired
	private  FolderService folderService;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (null == modelAndView) {
			return;
		}
		// 系统配置参数
		String basePath = HttpUtils.getBasePath(request);
		
		modelAndView.addObject("BASE_PATH", basePath);
//		String domain = ConfigUtil.getConfig("domain");
//		modelAndView.addObject("BASE_PATH", domain);
		modelAndView.addObject("UPLOAD_BASE_PATH", basePath + "/upload");
		modelAndView
				.addObject(
						"TEMPLATE_BASE_PATH",
						basePath
								+ "/static/template/"
								+ configService
										.getStringByKey(ConfigConstant.SHISHUO_TEMPLATE));
//		modelAndView
//		.addObject(
//				"TEMPLATE_BASE_PATH",
//				domain
//						+ "/static/template/"
//						+ configService
//								.getStringByKey(ConfigConstant.SHISHUO_TEMPLATE));
		modelAndView.addObject("shishuo_seo_title",
				configService.getStringByKey("shishuo_seo_title"));
		modelAndView.addObject("shishuo_seo_description",
				configService.getStringByKey("shishuo_seo_description"));
		modelAndView.addObject("shishuo_seo_headline",
				configService.getStringByKey("shishuo_seo_headline"));
		List<HeadlineVo> hvs = headlineService.getHeadlineList();
		
		List<ArticleVo> footInfoBarList=articleService.getArticleListOfNumByFolderEname("footinfo",100);//页脚
		Folder footFolder=folderService.getFolderByEname("footinfo");
		modelAndView.addObject("footFolder", footFolder);
		modelAndView.addObject("footInfoBarList", footInfoBarList);
		
		modelAndView.addObject("headlines", hvs);
		
		if(request.getSession().getAttribute("user")!=null)//用户id和用户对象 2015年8月4日 10:42:13 YangJ
		{
			User user=(User)(request.getSession().getAttribute("user"));
			modelAndView.addObject("user_id", user.getUser_id());
			modelAndView.addObject("userObj", user);
		}
		
		MDC.put("ip", HttpUtils.getIp(request));
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
